package uoc.ei.practica;

import uoc.ei.tads.Iterador;
import uoc.ei.tads.IteradorVectorImpl;
import uoc.ei.tads.Pila;
import uoc.ei.tads.PilaVectorImpl;

/**
 * clase que representa una partida en el sistema.
 *
 */
public class Game extends IdentifiedObject {
	
	/**
	 * primer jugador de la partida
	 */
	private Player player1;
	
	/**
	 * segundo jugador de la partida
	 */
	private Player player2;
	
	/**
	 * estado de la partida
	 */
	private State  state;
	
	/**
	 * letras de la partida
	 */
	private LetterGame[] letters;
	private int pLetters;
	
	/**
	 * pila de fichas
	 */
	private PilaVectorImpl<Card> cards;
	
	/** 
	 * campeonato de esta partida
	 */
	private ChampionShip championShip;
	private Board board;

	public Game(String gameId) {
		super(gameId);
		this.cards = new PilaVectorImpl<Card>(ScrabbleManager.MAX_CARDS_GAME);
		this.letters=new LetterGame[ScrabbleManager.MAX_LETTERS];
		this.pLetters=0;
		this.board = new Board();
	}
	
	public Game(ChampionShip championShip, String gameId, String player1, String player2) {
		this(gameId);
		this.championShip = championShip;
		this.player1=new Player(player1,null);
		this.player2=new Player(player2,null);
		this.state=State.CREATED;
		this.board = new Board();
		
	}

	/**
	 * método que añade una ficha a la pila de fichas de la partida
	 * @param la letra asociada a la ficha a crear
	 * @throws EIException lanza una excepción en el caso que
	 * la letra no exista o que se haya superado el máximo de letras.
	 */
	public void addCard(String l) throws EIException {
		Letter letter = championShip.getLetter(l);
		if (letter==null) throw new EIException(Messages.LETTER_NOT_FOUND);
		
		LetterGame lg= this.getLetter(l);
		if (lg==null) this.letters[this.pLetters++]=new LetterGame(letter);
		else {
			if (lg.count()<letter.count())
				lg.inc();
			else throw new EIException(Messages.MAX_LETTERS);
		}		
		this.cards.apilar(new Card(letter));

	}
	
	public Card pop() {
		return this.cards.desapilar();
	}

	/**
	 * método que inicializa una partida y asigna a cada jugador
	 * sus fichas de manera alternativa
	 */
	public void start() throws EIException {
		this.state=State.STARTED;
		
		
		int i=0;
		Card card=null;
		while (i<ScrabbleManager.MAX_CARDS_USER) {
			card = pop();
			player1.addCard(card);
			
			card = pop();
			player2.addCard(card);
			i++;
		}
		
	}

	public Iterador<Card> cardsPlayer(String playerId) {
		Iterador<Card> it=null;
		
		if (player1.is(playerId)) it = player1.cards();
		else if (player2.is(playerId)) it=player2.cards();
		else it=null;
		return it; 
	}	
	
	public Player findPlayer(String playerId)
	{
		if (player1.is(playerId)) return player1;
		else if (player2.is(playerId)) return player2;
		else return null;
		
	}
	public Player getPlayer(int player1or2)
	{
		if (player1or2==1) return player1;
		else if (player1or2==2) return player2;
		else return null;
		
	}

	public State getState() {
		return this.state;
	}
	
	public void  finishGame() {
		this.state=State.FINISHED;
	}
	
	public  LetterGame getLetter(String letter) {
		int i=0;
		LetterGame l = null;
		boolean found=false;
		
		while (i< pLetters && !found) {
			l = letters[i];
			found = l.is(letter);
			i++;
		}
		
		return (found?l:null);
	}
	


	public Iterador<LetterGame> letters() throws EIException {
		if (this.pLetters==0) throw new EIException(Messages.NO_LETTERS);
		
		Iterador<LetterGame> it =  new IteradorVectorImpl(this.letters,this.pLetters,0);
		return it;
		
	}

	public Iterador<Card> cards() {
		return this.cards.elementos();
	}

	public Board getBoard() {
		return this.board;
	}
	
	
	

	

}
